package com.fone.player.view;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.activity.main.MainTabActivity;
import com.fone.player.activity.main.SplashActivity;
import com.fone.player.client.LoginRst;
import com.fone.player.client.Reporter;
import com.fone.player.constant.FoneConstant;
import com.fone.player.play.FonePlayerActivity;
import com.fone.player.service.FoneServiceManager;
import com.fone.player.storage.SharedPreferenceModule;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.ScreenUtil;

@SuppressWarnings("unused")
public class ExitDialog implements OnClickListener {

	private static final String TAG = "ExitDialog";

	public static final int SUGGEST = 0;
	public static final int FORCE = 1;

	private TextView desc;
	private TextView title;
	private CustomDialog dialog;
	private Context mContext;
	private String updateDesc;

	public ExitDialog(Context mContext, Activity activity, String updateDesc) {
		this.updateDesc = updateDesc;
		this.mContext = mContext;
		View updateView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_exit_dialog, null);
		dialog = new CustomDialog((Context) activity, updateView);
		initDialogView(mContext, updateView);
		reportErrorInfo();
		Reporter.start();

	}

	private void initDialogView(Context mContext, View updateView) {
		dialog.setCanceledOnTouchOutside(false);

		dialog.show();
		// 记录升级框显示标志
		FoneConstant.isUpdate = true;
		if (getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT) {
			updateView.setMinimumWidth((int) (ScreenUtil
					.getScreenWidthPix(mContext) * 0.8));// 设置dialog的宽度
		} else {
			updateView.setMinimumWidth((int) (ScreenUtil
					.getScreenWidthPix(mContext) * 0.5));// 设置dialog的宽度
		}

		title = (TextView) updateView.findViewById(R.id.update_title);
		title.setText("退出");
		desc = (TextView) updateView.findViewById(R.id.update_content_tv);
		desc.setText(updateDesc);
		Button changeSureBtn = (Button) updateView
				.findViewById(R.id.update_sure_id);
		changeSureBtn.setOnClickListener(this);

		Button changeCancelBtn = (Button) updateView
				.findViewById(R.id.update_cancel_id);
		changeCancelBtn.setOnClickListener(this);
	}

	/**
	 * 上报出错的缓存信息
	 * 
	 * @return void
	 * @throws
	 */
	private void reportErrorInfo() {
		
		ArrayList<Long> errorCidList = StorageModule.getInstance()
				.getCIDListByError();
		
		if (errorCidList == null) {
			return;
		}
		
		if(!errorCidList.isEmpty()){
			for (int i = 0; i < errorCidList.size(); i++) {
				Reporter.logError(errorCidList.get(i) + "", 1);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_sure_id:
			// 退出应用
			ApplicationManage.getAplicationManageInstance().exitSystem();
			dialog.cancel();
			break;
		case R.id.update_cancel_id:
			dialog.cancel();
			break;
		}
	}

	/**
	 * 获取屏幕方向
	 * 
	 * @return
	 * @return int
	 * @throws
	 */
	private int getScreenOrientation() {
		return mContext.getResources().getConfiguration().orientation;
	}
}
